package se.webbzon.boltzmann.minigame.botany;

import java.awt.Rectangle;

import se.webbzon.boltzmann.configuration.Difficulty;
import se.webbzon.boltzmann.minigame.chemistry.AmmoniaMolecule;
import se.webbzon.boltzmann.minigame.chemistry.CarbonDioxideMolecule;
import se.webbzon.boltzmann.minigame.chemistry.NitrateMolecule;
import se.webbzon.boltzmann.minigame.chemistry.NitriteMolecule;
import se.webbzon.boltzmann.minigame.chemistry.NitrogenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.OxygenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SandBoxMolecule;
import se.webbzon.boltzmann.minigame.chemistry.WaterMolecule;
import se.webbzon.boltzmann.minigame.effect.SandBoxCloud;
import se.webbzon.boltzmann.minigame.effect.SandBoxRain;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxAnchor;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxContainer;
import se.webbzon.boltzmann.minigame.sandbox.ReadOnlySandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxAnchor;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxArrow;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxItem;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxPicture;
import se.webbzon.boltzmann.resource.Resources;

public class NitrogenCycle extends Cycle {
	
	/*============================================================
	Protected Methods
	============================================================*/

	@Override protected void onCreate(ReadOnlySandBox box) {
		Rectangle r = new Rectangle();
		box.getBoundary(r);
		double x0 = 0, y0 = 0; //r.getMinX(), y0 = r.getMinY();
		double w = r.getWidth(), h = r.getHeight();
		
		DefaultSandBoxContainer n2, nh3, no2, no3; 
		n2 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		n2.setPosition(x0 + 320, y0 + 48);
		nh3 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		nh3.setPosition(x0 + 544, y0 + 200);
		no2 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		no2.setPosition(x0 + 320, y0 + 200);
		no3 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		no3.setPosition(x0 + 128, y0 + 200);
		
		SandBoxPicture plant, b1, b2, b3, b4;
		plant = new SandBoxPicture(Resources.getImageSet("biology_plant"),50);
		plant.setPosition(x0 + 128, y0 + 84);
		plant.setSize(96,96);
		plant.setSpeed(0.02);
		b1 = new SandBoxPicture(Resources.getImageSet("biology_bacteria"),50);
		b1.setSize(32, 32);
		b1.setPosition(x0 + 64, y0 + 132);
		b2 = new SandBoxPicture(Resources.getImageSet("biology_bacteria"),50);
		b2.setSize(32, 32);
		b2.setPosition(x0 + 544, y0 + 132);
		b3 = new SandBoxPicture(Resources.getImageSet("biology_bacteria"),50);
		b3.setPosition(x0 + 256, y0 + 156);
		b4 = new SandBoxPicture(Resources.getImageSet("biology_bacteria"),50);
		b4.setPosition(x0 + 400, y0 + 156);
		
		SandBoxCloud cloud = new SandBoxCloud();
		cloud.setPosition(x0 + 544, y0 + 48);
		SandBoxRain rain = new SandBoxRain();
		rain.setPosition(x0 + 544, y0 + 48);
		rain.setSize(128, 84);
		Earth earth = new Earth();
		earth.setSize(earth.getWidth(), 132);
		
		/** Add anchors and arrows**/
		SandBoxAnchor an1 = new DefaultSandBoxAnchor(x0 + 64, y0 + 48);
		SandBoxAnchor an2 = new DefaultSandBoxAnchor(x0 + 544, y0 + 48);
		SandBoxAnchor an3 = new DefaultSandBoxAnchor(x0 + 64, y0 + 200);
		
		SandBoxArrow[] arrows = {
				new SandBoxArrow(plant,b2),
				new SandBoxArrow(b2,nh3),
				new SandBoxArrow(nh3,no2),
				new SandBoxArrow(no2,no3),
				new SandBoxArrow(no3,b1),
				new SandBoxArrow(b1,n2),
				new SandBoxArrow(n2,b2),
				new SandBoxArrow(no3,plant)
		};
		arrows[4].addAnchor(an3);
		arrows[5].addAnchor(an1);
		arrows[6].addAnchor(an2);
		
		/** Add items to cycle **/
		SandBoxItem[] items = {
				n2, nh3, no2, no3,
				plant, cloud, rain, earth,
				b1, b2, b3, b4
		};
		
		for (SandBoxItem item : items)
			addItem(item);
		for (SandBoxItem item : arrows)
			addItem(item);
		
		/** Create and add molecules. **/
		SandBoxMolecule[] molecules = {
				new WaterMolecule(), new CarbonDioxideMolecule(),
				new OxygenMolecule(), new NitrateMolecule(),
				new NitrogenMolecule(), new AmmoniaMolecule(),
				new NitriteMolecule()
			};
		for (SandBoxMolecule m : molecules) {
			m.setPosition(	x0 + (0.1 + 0.8 * w * Math.random()), 
							y0 + (0.1 + 0.8 * h * Math.random()));
			m.setLifetime(-1);
			addItem(m);
		}
		
		/** Add solutions **/
		addSolution(n2,NitrogenMolecule.class);
		addSolution(nh3,AmmoniaMolecule.class);
		addSolution(no2,NitriteMolecule.class);
		addSolution(no3,NitrateMolecule.class);
	}

	@Override protected void onDestroy(ReadOnlySandBox box) {
		clearSolution();
		clearItems();
	}
	
	@Override public Difficulty getDifficulty() {
		return Difficulty.MEDIUM;
	}

}
